package xyz.syyrjx.blog.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

//@Aspect
//@Component
//public class LogAspect {
//
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    private class Request{
//        private String url;
//        private String ip;
//        private String method;
//        private Object[] args;
//
//        public Request() {}
//
//        public Request(String url, String ip, String method, Object[] args) {
//            this.url = url;
//            this.ip = ip;
//            this.method = method;
//            this.args = args;
//        }
//
//        @Override
//        public String toString() {
//            return "Request{" +
//                    "url='" + url + '\'' +
//                    ", ip='" + ip + '\'' +
//                    ", method='" + method + '\'' +
//                    ", args=" + Arrays.toString(args) +
//                    '}';
//        }
//    }
//
//
//    //记录访问时间,访问的url
//    @Before("execution(public * xyz.syyrjx.blog.controller.GuestController.*(..))")
//    public void doBefore(JoinPoint point){
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String url = request.getRequestURL().toString();
//        String ip = request.getRemoteAddr();
//        String method = point.getSignature().getDeclaringTypeName() + "." + point.getSignature().getName();
//        Object[] args = point.getArgs();
//        Request req = new Request(url,ip,method,args);
//        logger.info("request[" + req.toString() + "]");
//    }
//
//
//}
